package com.tang.thepath;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.service.JWTHolder;

import java.util.ArrayList;
import java.util.List;

public class ProblemListActivity extends BaseActivity {

    private List<RequestModel.Problem> dataList = new ArrayList<>();
    private ProblemListActivity.ListAdapter adapter;
    private ListView listView;
    private int page = 1;
    private boolean hasMore = true;
    private int pageSize = 20;
    private int lastItem = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem_list);

        listView = (ListView) findViewById(R.id.listView);
        adapter = new ProblemListActivity.ListAdapter();

        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            //AbsListView view 这个view对象就是listview
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {
                        loadPage(page++);
                    }
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItem = firstVisibleItem + visibleItemCount - 1 ;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        hasMore = true;
        dataList.clear();
        page = 1;
        this.loadPage(page++);
    }


    protected void loadPage(int page){
        if(!hasMore){
            if(dataList.size() > pageSize){
                //toast("没有更多数据了");
            }
            return;
        }
        showLoading("正在加载");
        BizService.listProblem(page).subscribe(new HttpSubscriber<RequestModel.ListProblemResponse>() {
            @Override
            protected void onSuccess(RequestModel.ListProblemResponse response) {
                if(response.problemList.size() < pageSize){
                    hasMore = false;
                }
                dismisLoading();
                dataList.addAll(response.problemList);
                adapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if(itemId == R.id.menu_add){
            JWTHolder holder = JWTHolder.getInstance();
            if(holder.getRole() == 0){
                startActivity(new Intent(getContext(), ProblemActivity.class));
            }else{
                toast("您没有权限使用该功能");
            }
        }else if(itemId == android.R.id.home){
            finish();
        }
        return true;
    }

    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int i) {
            return dataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return  dataList.get(i) != null ? dataList.get(i).id : 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ProblemListActivity.ViewHolder holder = null;
            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.item_problem,null);
                /*if(position == (customInfoList.size()+1)){
                    convertView = inflater.inflate(R.layout.item_custom_info,null);
                }else{
                    convertView = inflater.inflate(R.layout.item_custom_info,null);
                }*/


                holder = new ProblemListActivity.ViewHolder();
                holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
                holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
                holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
                convertView.setTag(holder);

            }else{
                holder = (ProblemListActivity.ViewHolder)convertView.getTag();
            }

            RequestModel.Problem obj =(RequestModel.Problem) getItem(position);
            holder.tv1.setText(obj.title);
            holder.tv2.setText(obj.content);
            holder.tv3.setText(obj.createdAt);
            return convertView;
        }
    }

    class ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
    }

}
